<template>
	<view style="height: 100%;">
		<swiper-tabs height="100%" barWidth="240">
			<template v-slot:tabs style="height: 100%;">
				<u-tabs-swiper name="name" :barWidth="barWidth" active-color="#333333" inactive-color="#666666"
					bg-color="none" ref="tabs1" :list="parames.tabs" :current="current" @change="tabsChange"
					:is-scroll="false" swiperWidth="750"></u-tabs-swiper>
				<swiper style="height: calc(100% - 84rpx);" :current="swiperCurrent" @transition="transition"
					@animationfinish="animationfinish" class="swiper">
					<slot name="swiperTbs'"></slot>
					<swiper-item :style="swiperItemStyle" class="swiper-item" v-for="(item, index) in parames.tabs"
						:key="index">
						<scroll-view scroll-y class="scroll" @scrolltolower="loadmore">
							<card @reset="reset" :list="parames.list" :swiperCurrent="swiperCurrent"
								v-if="parames.list.length" />
							<view class="def" v-else>
								<view style="margin: 120rpx 0 80rpx 0;text-align: center;">您目前没有活动报名</view>
								<view class="def-btn" style="margin-left: 57rpx;" @click="applyNow">即刻报名</view>
							</view>
							<u-loadmore bg-color="none" :status="parames.loadStatus" @loadmore="loadmore"
								v-if="parames.list.length"></u-loadmore>
						</scroll-view>
					</swiper-item>
				</swiper>

			</template>
		</swiper-tabs>
	</view>
</template>

<script lang="ts" setup>
	import swiperTabs from '@/components/swiperTabs.vue'
	import card from './components/card.vue'
	import {
		reactive, ref, watch
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import me from '../../../api/me.js';
	let tabs1 = ref()
	let current = ref(0)
	let swiperCurrent = ref(0)
	let parames = reactive({
		list: [],
		tabs: [
			{
				name: '即将到来的活动'
			}, {
				name: '我的活动历史记录'
			}
		],
		page: 1,
		loadStatus: ''
	})
	watch(() => swiperCurrent.value, (index) => {
		reset()
	})
	const reset = () => {
		parames.list = []
		parames.page = 1
		activeList()
	}
	const moreStatus = (status : string) => {
		parames.loadStatus = status
	}
	const location = () => {
		uni.getLocation({
			type: 'gcj02',
			geocode: true,
			success: res => {
				uni.setStorageSync('address', {
					...uni.getStorageSync('address'),
					lat: res.latitude,
					lng: res.longitude,
				})
				activeList({
					...uni.getStorageSync('address'),
					lat: res.latitude,
					lng: res.longitude,
				})
			},
			fail: () => {
				activeList()
			}

		})
	}
	const activeList = async (addr ?: any) => {
		addr = addr ? addr : uni.getStorageSync('address')
		const res = await me.activeList({
			type: swiperCurrent.value + 1,
			lat: addr.lat || 0,
			lng: addr.lng || 0,
			page: parames.page,
			size: 10
		})
		res.data.map(v => {
			v.timestamp = differenceTime(v.createtime || 0) * 1000
		})
		parames.list = [...parames.list, ...res.data]
		if (res.data.length < 10) return moreStatus('nomore')
		if (res.data.total <= 10) {
			moreStatus('nomore')
		} else {
			moreStatus(res.data.length !== 0 ? 'loadmore' : 'nomore')
		}
		if (res.data.length === 0) return false
		parames.page += 1 // 当前页加1
	}
	const loadmore = () => {
		if (parames.loadStatus == 'nomore') return
		location()
	}
	const applyNow = () => {
		uni.navigateTo({
			url: '/pages/common/salon/index'
		})
	}
	// tabs通知swiper切换
	const tabsChange = (index : number) => {
		swiperCurrent.value = index;
	}
	// swiper-item左右移动，通知tabs的滑块跟随移动
	const transition = (e) => {
		let dx = e.detail.dx;
		tabs1.value.setDx(dx);
	}
	const differenceTime = (time : any) => {
		// @ts-ignore
		let timestamp : any = (Date.parse(new Date())) / 1000; //计算当前时间戳
		// @ts-ignore
		let pastTime : any = (Date.parse(new Date(time))) / 1000 //创建时间
		let endtime = pastTime + 1800
		return endtime > timestamp ? endtime - timestamp : 0
	}
	const animationfinish = (e) => {
		let current2 = Number(e.detail.current)
		tabs1.value.setFinishCurrent(current2);
		swiperCurrent.value = current2;
		current.value = current2;
	}
	onLoad(() => {
		location()
	})
</script>

<style>
	page {
		height: 100%;

	}
</style>
<style lang="scss">
	.def {
		width: 654rpx;
		height: 393rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 4rpx 16rpx 3rpx rgba(225, 225, 225, 0.5);
		border: 2rpx solid #EEEFF0;
		position: absolute;
		top: 200rpx;
		left: 48rpx;

		.def-btn {
			text-align: center;
			width: 544rpx;
			height: 92rpx;
			background: #000000;
			line-height: 92rpx;
			color: #fff;
		}
	}
</style>